import { selectCompany } from '@/apis/company/audit'
import { invoiceList, invoiceParams } from '@/apis/finance/invoice'
import router from '@/router'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { reactive, ref } from 'vue'
import * as tableConfig from './config.json'

export class Invocie extends BaseData implements BaseMethod {
    //搜索数据
    searchData = reactive({
        invoiceCompany: '',
        status: '1' //发票状态 1.开票中；2.开票成功
    })
    //
    company = ref('')
    companyId = ref('')
    companyOptions: any[] = reactive([])
    loading = ref(false)
    constructor() {
        super(tableConfig)

        this.getData()
    }
    onOperation(arg?: any): void {
        const { type, data } = arg
        router.push({
            path: '/finance/invoice/look',
            query: { type, id: data.id }
        })
    }
    /**
     * 监听用户输入的公司名，搜索
     * @param query
     */
    remoteMethod = (query: string) => {
        if (query) {
            this.loading.value = true
            this.getCompanyList(query, () => {
                this.loading.value = false
            })
        }
    }

    /**
     * 监听选择的公司
     * @param res
     */
    onSelectCompany = (res: any) => {
        console.log(res)
        this.companyId.value = res
    }
    /**
     * 获取公司
     * @param name
     * @param callback
     */
    getCompanyList(name = '', callback = () => {}) {
        selectCompany({ current: 1, size: 10, companyParam: name }).then(res => {
            if (res.data.value === 200) {
                this.companyOptions.length = 0
                res.data.data.records.map((item: any) => {
                    this.companyOptions.push({ value: item.companyId, label: `${item.companyName}` })
                })
                // this.pageData.table.length = 0
                // this.pageData.table.push(...res.data.data.records)
                // this.pageData.pagination.total = res.data.data.total
                callback()
            }
        })
    }
    /**
     * 拉取数据
     */
    getData = () => {
        const obj: invoiceParams = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        // if (this.companyId.value) {
        //     this.companyOptions.forEach((item: any) => {
        //         if (item.value === this.companyId.value) {
        //             obj.invoiceCompany = item.label
        //         }
        //     })
        // }

        if (this.searchData.invoiceCompany) {
            obj.invoiceCompany = this.searchData.invoiceCompany
        }
        console.log(obj, this.searchData.invoiceCompany)
        if (Number(this.searchData.status)) {
            obj.invoiceStatus = Number(this.searchData.status)
        }
        invoiceList(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.data.total
                this.pageData.table.data.push(...res.data.data.records)
            }
        })
    }
    /**
     * 监听搜索
     */
    onSearch = (res: any) => {
        this.searchData.status = res.status
        this.searchData.invoiceCompany = res.invoiceCompany
        this.getData()
    }
    /**
     * 监听点击重置按钮
     */
    onReset = () => {
        this.searchData.status = '0'
        this.searchData.invoiceCompany = ''
        this.pageData.pagination.pageNo = 1
        this.getData()
    }
    /**
     * 监听分页发生改变
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }
    /**
     * 监听选择每页显示个数
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }
}
